package controller.business.managerAssess;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import itext.ItextFont;
import wt.entity.business.ManagerAssess;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ManagerAssessPdf {

    public static Map<ManagerAssess, List<ManagerAssess>> managerAssesList2Map(List<ManagerAssess> list) {
        Map<ManagerAssess, List<ManagerAssess>> managerAssessListMap = new LinkedHashMap<ManagerAssess, List<ManagerAssess>>();
        String rootId =null;
        for (ManagerAssess managerAssess : list) {
            if (managerAssess.getAccessTypePid()==null) {
                rootId=managerAssess.getAccessTypeId();
                break;
            }
        }

        for (ManagerAssess managerAssess1 : list) {
            if (rootId.equals(managerAssess1.getAccessTypePid())) {
                List<ManagerAssess> temp = new ArrayList<ManagerAssess>();
                for (ManagerAssess managerAssess2 : list) {
                    if (managerAssess1.getAccessTypeId().equals(managerAssess2.getAccessTypePid())) {
                        temp.add(managerAssess2);
                    }
                }
                managerAssessListMap.put(managerAssess1, temp);
            }
        }
        return managerAssessListMap;
    }

    private static  List<PdfPCell>  printContent(List<ManagerAssess> list) {

        Map<ManagerAssess, List<ManagerAssess>> managerAssessListMap = managerAssesList2Map(list);
        
        List<PdfPCell>  cells = new ArrayList<PdfPCell>();
        
        int u = 0;
        for (Map.Entry<ManagerAssess, List<ManagerAssess>> assessListEntry : managerAssessListMap.entrySet()) {
            u++;
            int size = assessListEntry.getValue().size();

            PdfPCell cell=  new PdfPCell(new Phrase(u+"", ItextFont.fangSongNormalPdf10));
            cell.setRowspan(size);
            cells.add(cell);

            cell=  new PdfPCell(new Phrase(assessListEntry.getKey().getAccessTypeName(), ItextFont.fangSongNormalPdf10));
            cell.setRowspan(size);
            cells.add(cell);


            for (int i = 0; i < size; i++) {
                cell=  new PdfPCell(new Phrase((i + 1)+"", ItextFont.fangSongNormalPdf10));
                cells.add(cell);
                cell=  new PdfPCell(new Phrase(assessListEntry.getValue().get(i).getAccessTypeName(), ItextFont.fangSongNormalPdf10));
                cells.add(cell);
                cell=  new PdfPCell(new Phrase(assessListEntry.getValue().get(i).getResult(), ItextFont.fangSongNormalPdf10));
                cells.add(cell);
            }
        }
        return cells;
    }




    public static void createPdf(OutputStream outputStream,List<ManagerAssess> list) throws DocumentException, IOException {

        //设置纸张大小
        Document document = new Document(PageSize.A4);
        //建立一个书写器，与document对象关联
        PdfWriter.getInstance(document, outputStream);
        document.open();

        Paragraph paragraph = new Paragraph("申报材料质量审核评价记录及审核意见", ItextFont.fangZhengPdf18);
        paragraph.setAlignment(Element.ALIGN_CENTER);
        document.add(paragraph);


        //设置Table表格,创建一个7列的表格
        PdfPTable table = new PdfPTable(5);
        float width[] = {1,2, 1, 10, 2};//设置每列宽度比例
        table.setWidths(width);
        table.setWidthPercentage(100);//占页面宽度比例

        PdfPCell[] cell = new PdfPCell[6];
        cell[0] = new PdfPCell(new Phrase("序号", ItextFont.fangSongBoldPdf10));
        cell[1] = new PdfPCell(new Phrase("材料名称", ItextFont.fangSongBoldPdf10));
        cell[2] = new PdfPCell(new Phrase("评价内容（不得出现以下情况）", ItextFont.fangSongBoldPdf10));
        cell[2].setColspan(2);
        cell[3] = new PdfPCell(new Phrase("评价记录", ItextFont.fangSongBoldPdf10));

        for (int i = 0; i <4; i++) {
            cell[i].setHorizontalAlignment(Element.ALIGN_CENTER);//居中显示
            cell[i].setVerticalAlignment(Element.ALIGN_MIDDLE);//纵向居中显示
            cell[i].setPaddingTop(9F);
            cell[i].setPaddingBottom(9F);
            table.addCell(cell[i]);
        }

        List<PdfPCell>  pdfPCells= printContent(list);

        for(PdfPCell pdfPCell:pdfPCells){

            pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);//居中显示
            pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);//纵向居中显示
            pdfPCell.setPaddingTop(9F);
            pdfPCell.setPaddingBottom(9F);
            table.addCell(pdfPCell);
        }


        paragraph = new Paragraph();
        paragraph.setSpacingBefore(5f);
        paragraph.add(table);
        document.add(paragraph);

        paragraph = new Paragraph("审核人：               日期：                审查组织单位（加盖公章）：" +
                "\n说明：“评价记录”栏填写“Y”（通过），“N”（不通过），“—”（不适用）。", ItextFont.fangSongNormalPdf10);
        paragraph.setSpacingBefore(3f);
        document.add(paragraph);
        document.close();

    }
}



